package com.ryhx.main;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.ryhx.util.ConfigUtil;

/**
 * controller 文件生成器
 * @author yaojinshan
 * @since 2017-08-04 17:49
 *
 */
public class ControllerFileProducer {
	static FileWriter fw;
	public static void main(String[] args) {
		FileDirProducer.createDir();
		createControllerFile();
	}
	/**
	 * 生成 controller 文件
	 */
	public static void createControllerFile() {

		//System.out.println("controller文件：暂时不生成");
		try {
			// 按行读取DaoTemplate的内容
			String controllerTemplateFileSourceString = DaoAndServiceProducer.readFileByLines("template/TemplateController.txt");
			controllerTemplateFileSourceString = controllerTemplateFileSourceString.replace("package ","package " + ConfigUtil.controllerPackagePath); // 替换第一行的包名
			controllerTemplateFileSourceString = controllerTemplateFileSourceString.replaceAll("simpleModelNameLower",ConfigUtil.simpleModelNameLower);// 替换所有小写的实体命名
			controllerTemplateFileSourceString = controllerTemplateFileSourceString.replaceAll("firstCharLowerSimpleModelName", ConfigUtil.firstCharLowerSimpleModelName);// 替换掉所有首字母小写的实体命名
			controllerTemplateFileSourceString = controllerTemplateFileSourceString.replaceAll("SimpleModelName", ConfigUtil.simpleModelName);// 替换掉所有类名称
			controllerTemplateFileSourceString = controllerTemplateFileSourceString.replaceAll("entityName",ConfigUtil.entityName);// 替换掉所有提示信息变量
			writeControllerContentToControllerFile(controllerTemplateFileSourceString);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 写入 controller 内容到 文件中
	 * @param controllerTemplateFileSourceString 
	 * @throws Exception 
	 */
	private static void writeControllerContentToControllerFile(String controllerTemplateFileSourceString) throws Exception {
		File controllerFile = new File("source/" + ConfigUtil.simpleModelNameLower + "/controller/" + ConfigUtil.simpleModelName + "Controller.java");
		if (!controllerFile.exists()) {
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的controller文件:" + controllerFile.getName() + "创建");
			controllerFile.createNewFile();
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的controller文件:" + controllerFile.getName() + "创建完成");
		}
		System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Controller文件:" + controllerFile.getName() + "写入");
		fw = new FileWriter(controllerFile);
		fw.write(controllerTemplateFileSourceString);
		System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Controller文件:" + controllerFile.getName() + "写入完成");
	}
}
